package com.bRoom.controller.front;

import com.bRoom.common.Conts;
import com.bRoom.common.ServerResponse;
import com.bRoom.pojo.Car;
import com.bRoom.pojo.User;
import com.bRoom.service.ICarService;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("front/car")
public class ICarController {

   @Autowired
    private ICarService iCarService;


   @RequestMapping("add_cart.do")
   @ResponseBody
   public ServerResponse<Car> addCar (HttpSession session,Car car){
       User user=(User) session.getAttribute(Conts.CURRENT_USER);
       if (user==null){
            return ServerResponse.createByErrorMsg("用户未登录，请先登录");
       }
          return iCarService.addCar(car,user.getUserId());
   }


    @RequestMapping("update_car.do")
    @ResponseBody
   public  ServerResponse<Car> updateCar(HttpSession session,Car car){
       User user=(User) session.getAttribute(Conts.CURRENT_USER);
       if (user==null){
           return ServerResponse.createByErrorMsg("用户未登录，请先登录");
       }
       return iCarService.updateCar(car,user.getUserId());
   }

    @RequestMapping("carList.do")
    @ResponseBody
   public ServerResponse<PageInfo> carList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "5") int pageSize){
       User user=(User) session.getAttribute(Conts.CURRENT_USER);
       if (user==null){
           return ServerResponse.createByErrorMsg("用户未登录，请先登录");
       }
       return iCarService.carList(user,pageNum,pageSize);
   }
}
